//
//  MBGoodsDetailCommentListView.m
//  MobellCloud_ios
//
//  Created by cherry pu on 2017/12/28.
//  Copyright © 2017年 zhangXin. All rights reserved.
//

#import "MBGoodsDetailCommentListView.h"
#import "MBImageGalleryViewController.h"
#import "MBGoodsDetailCommentTableViewCell.h"
#import "MBRefreshFooterView.h"
#import "MBNoCommunityDataView.h"
#import "MBGoodsNetTool.h"
#import "MBCommentModel.h"

static NSString* GOODS_DETAIL_COMMENT_LIST_CELL = @"GOODS_DETAIL_COMMENT_LIST_CELL";

@interface MBGoodsDetailCommentListView ()<UITableViewDelegate,UITableViewDataSource,MBGoodsDetailCommentTableViewCellDelegate>
@property (nonatomic,weak)UITableView* tableView;
@property (nonatomic,strong) NSMutableArray* dataArray;
@property (nonatomic,weak)MBRefreshFooterView* footerView;
@property (nonatomic,assign)int page;
@end

@implementation MBGoodsDetailCommentListView

- (instancetype)init
{
    self = [super init];
    if (self) {
        UITableView* tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        tableView.backgroundColor = COLOR_BG;
        MBNoCommunityDataView* dataView = [[MBNoCommunityDataView alloc]initWithFrame:tableView.frame];
        dataView.title = @"暂无晒单";
        tableView.backgroundView = dataView;
        tableView.backgroundView.hidden = YES;
        self.tableView = tableView;
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [tableView registerClass:[MBGoodsDetailCommentTableViewCell class] forCellReuseIdentifier:GOODS_DETAIL_COMMENT_LIST_CELL];
        MBRefreshFooterView* footerView = [[MBRefreshFooterView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, [UIView lf_sizeFromIphone6:58])];
        self.footerView = footerView;
        tableView.tableFooterView = footerView;
        tableView.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.01)];
        [self addSubview:tableView];
        
        [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self).insets(UIEdgeInsetsZero);
        }];

        MJRefreshNormalHeader * activityHeader = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            self.page = 1;
            [self getGoodsCommentWithPage:self.page];
        }];
        tableView.mj_header = activityHeader;
        [activityHeader setTitle:@"下拉即可刷新" forState:MJRefreshStateIdle];
        [activityHeader setTitle:@"松开即可更新" forState:MJRefreshStatePulling];
        [activityHeader setTitle:@"加载中" forState:MJRefreshStateRefreshing];
        activityHeader.lastUpdatedTimeLabel.hidden = YES;
        [tableView.mj_header beginRefreshing];
        
        MJRefreshBackNormalFooter * knowledgeFooter = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            [self getGoodsCommentWithPage:self.page];
        }];
        
        tableView.mj_footer = knowledgeFooter;

    }
    return self;
}

-(NSMutableArray *)dataArray
{
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

-(void)getGoodsCommentWithPage:(int)page
{
    self.footerView.type = kLoadingType;
    
    MBLoginUserModel* userModel = [MBLoginUserModelArchieveTool userInfoUnAchieveFromFile];
    MBGoodsInfoParam* param = [[MBGoodsInfoParam alloc]init];
    param.p = page;
    param.goods_id = self.infoModel.goods.goods_id;
    if (userModel.token && ![userModel.token isEqualToString:@""]) {
        param.token = userModel.token;
    }
    [MBGoodsNetTool netToolGoodsCommentWithParam:param success:^(MBHTTPResponse *response) {
        if (response.status == 1) {
            NSArray* array = response.result;
            NSMutableArray* list = [NSMutableArray array];
            for (int i=0; i<array.count; i++) {
                MBCommentModel* commentModel = [MBCommentModel yy_modelWithJSON:array[i]];
                [list addObject:commentModel];
            }
            if (page == 1) {
                self.dataArray = list;
            }else {
                [self.dataArray addObjectsFromArray:list];
            }
            [self.tableView reloadData];
            self.page ++;
            if (list.count <10) {
                self.tableView.mj_footer.hidden = YES;
            }else {
                self.tableView.mj_footer.hidden = NO;
            }
        }else {
            [InterfaceHUD showErrorHUDWithString:response.msg];
        }
        if (page != 1) {
            [self.tableView.mj_footer endRefreshing];
        }else {
            [self.tableView.mj_header endRefreshing];
        }
        self.footerView.type = kEndLoadType;
    } failure:^(NSError *error) {
        [InterfaceHUD showErrorHUDWithString:@"获取商品评论失败，请稍后重试"];
        if (page != 1) {
            [self.tableView.mj_footer endRefreshing];
        }else {
            [self.tableView.mj_header endRefreshing];
        }
        self.footerView.type = kEndLoadType;
    }];

}

#pragma mark UITableView Delegate Datasource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MBGoodsDetailCommentTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:GOODS_DETAIL_COMMENT_LIST_CELL forIndexPath:indexPath];
    cell.commentModel = self.dataArray[indexPath.row];
    cell.delegate = self;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [tableView fd_heightForCellWithIdentifier:GOODS_DETAIL_COMMENT_LIST_CELL cacheByIndexPath:indexPath configuration:^(id cell) {
        MBGoodsDetailCommentTableViewCell* commentCell = cell;
        commentCell.commentModel = self.dataArray[indexPath.row];
    }];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}


-(void)setInfoModel:(MBGoodsInfoModel *)infoModel
{
    _infoModel = infoModel;
    if (infoModel.comment.count == 0) {
        self.tableView.backgroundView.hidden = NO;
        self.tableView.tableFooterView.hidden = YES;
    }else {
        self.tableView.backgroundView.hidden = YES;
        self.tableView.tableFooterView.hidden = NO;
    }
    [self.tableView reloadData];
}

#pragma mark - MBGoodsDetailCommentTableViewCellDelegate
-(void)goodsDetailCommentCellClickImageWithIndex:(int)index withModel:(MBCommentModel *)commentModel{
    if (self.delegate && [self.delegate respondsToSelector:@selector(goodsDetailCommentListViewClickImageWithCommentModel:andIndex:)]) {
        [self.delegate goodsDetailCommentListViewClickImageWithCommentModel:commentModel andIndex:index];
    }
}


@end
